/* 
 * File:   IntegerConverter.cpp
 * Author: Gab'z
 * 
 * Created on 4 juin 2012, 09:39
 */

#include "IntegerConverter.h"
#include "../../../Context.h"
#include "../../literal/Complex.h"
#include "../../literal/SimpleNumber.h"
#include "../../literal/Integer.h"

namespace calculator {

    IntegerConverter::IntegerConverter(const std::string name) : UnaryOperator(name) {
    }

    IntegerConverter::IntegerConverter(const IntegerConverter& orig) : UnaryOperator(orig._name) {
    }

    IntegerConverter::~IntegerConverter() {
    }

    std::string IntegerConverter::isExecutable() const {
        if (Context::getInstance()->getCurrentStack()->size() == 0) return "Empty stack";
        return "";
    }

    IntegerConverter* IntegerConverter::clone() const {
        return new IntegerConverter(_name);
    }

    const Number* IntegerConverter::apply(const Number* number) const throw (ArithmeticException) {
        const Complex* complex = dynamic_cast<const Complex*> (number);
        const SimpleNumber* sn = dynamic_cast<const SimpleNumber*> (number);
        if (complex != 0) return new Integer(((int) complex->getRe()->value()));
        else if (sn != 0) return new Integer((int) sn->value());
        throw ArithmeticException("NaN");
    }
}